package com.yjz.study.adapter.detail

import android.content.res.ColorStateList
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import com.yjz.common.extend.loadCircle
import com.yjz.log.util.DisplayUtil
import com.yjz.study.R
import com.yjz.study.http.entity.res.GoodsDetailModel
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.recycler.item.HiViewHolder
import kotlin.math.min

/**
 *作用 : 商品详情评论模块
 *作者 : yjz
 *时间 : 2025/5/14 16:40
 */
class GoodsDetailCommentItem(private val model: GoodsDetailModel) : HiDataItem<GoodsDetailModel, HiViewHolder>() {
    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context ?: return

        val commentTitleTv = holder.findViewById<TextView>(R.id.comment_title)
        commentTitleTv?.text = model.commentCountTitle

        // 流式布局
        val chipGroup = holder.findViewById<ChipGroup>(R.id.chip_group)
        if (!TextUtils.isEmpty(model.commentTags)) {
            chipGroup?.visibility = View.VISIBLE
            val contains = model.commentTags.contains(" ")
            if (contains) {
                val stringList = model.commentTags.split(" ")
                for (index in stringList.indices) {
                    // 缓存复用的情况判断
                    val chip: Chip = if (index < chipGroup?.childCount!!) {
                        chipGroup.getChildAt(index) as Chip
                    } else {
                        val chipLabel = Chip(context)
                        chipLabel.chipCornerRadius = DisplayUtil.dp2px(4f).toFloat()
                        chipLabel.chipBackgroundColor =
                            ColorStateList.valueOf(
                                ContextCompat.getColor(
                                    context,
                                    com.yjz.ui.R.color.color_eed
                                )
                            )
                        chipLabel.setTextColor(ContextCompat.getColor(context, com.yjz.ui.R.color.color_000))
                        chipLabel.textSize = 14f
                        chipLabel.gravity = Gravity.CENTER
                        chipLabel.isCheckedIconVisible = false
                        chipLabel.isCheckable = false
                        chipLabel.isChipIconVisible = false
                        chipGroup.addView(chipLabel)
                        chipLabel
                    }
                    chip.text = stringList[index]
                }
            }
        } else {
            chipGroup?.visibility = View.GONE
        }

        // 评论容器
        val commentContainerLl = holder.findViewById<LinearLayout>(R.id.comment_container)
        if (model.commentModels.isNullOrEmpty()) {
            commentContainerLl?.visibility = View.GONE
            return
        }

        // 动态添加评论信息
        commentContainerLl?.visibility = View.VISIBLE
        val commentModels = model.commentModels
        // 只多显示6条
        for (index in 0..min(commentModels.size - 1, 6)) {
            val commentItem = if (index < commentContainerLl?.childCount!!) {
                commentContainerLl.getChildAt(index)
            } else {
                val commentItem = LayoutInflater.from(context).inflate(R.layout.layout_detail_item_comment_item, commentContainerLl, false)
                commentContainerLl.addView(commentItem)
                commentItem
            }
            val commentModel = commentModels[index]

            val userNameTv = commentItem.findViewById<TextView>(R.id.user_name)
            userNameTv.text = commentModel.nickName
            val commentContentTv = commentItem.findViewById<TextView>(R.id.comment_content)
            commentContentTv.text = commentModel.content
            val userAvatar = commentItem.findViewById<ImageView>(R.id.user_avatar)
            userAvatar.loadCircle(commentModel.avatar)
        }
    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_comment_item
    }
}